/**
 * 账单管理初始化
 */
var TMonthbalanceDetail = {
    id: "TMonthbalanceDetailTable",	//表格id
    seItem: null,		//选中的条目
    table: null,
    layerIndex: -1
};

/**
 * 初始化表格的列
 */
TMonthbalanceDetail.initColumn = function () {
    return [
        {type: 'checkbox', fixed: 'left'},
        {title: '账单号', field: 'id', visible: true, align: 'center', valign: 'middle'},
        {title: '来源', field: 'income', visible: true, align: 'center', valign: 'middle',
            templet: function (d) {
                if(d.income==1){
                    return "收货"
                }else if(d.income==2){
                    return "自提"
                }else if(d.income==3){
                    return "配送"
                }else if(d.income==4){
                    return "中转"
                }else if(d.income==5){
                    return "改单"
                }else if(d.income==6){
                    return "抹平"
                }else if(d.income==7){
                    return "转结"
                }else if(d.income==8){
                    return "新转结"
                }
                return ""

            }},
        {title: '账单状态', field: 'status', visible: true, align: 'center', valign: 'middle',
            templet: function (d) {
                if(d.status==0){
                    return "待结算"
                }
                return "已结算"

            }
        },
        {title: '车次号', field: 'income_name', visible: true, align: 'center', valign: 'middle'},
        {title: '网点/收货人', field: 'traner', visible: true, align: 'center', valign: 'middle'},
        {title: '应收金额', field: 'money', visible: true, align: 'center', valign: 'middle'},
        {title: '账单日期', field: 'createtime', visible: true, align: 'center', valign: 'middle'},
        {title: '创建人员', field: 'name', visible: true, align: 'center', valign: 'middle'},
        {title: '制单员', field: 'dotId', visible: true, align: 'center', valign: 'middle'},
        {title: '备注', field: 'remark', visible: true, align: 'center', valign: 'middle'}
    ];
};

TMonthbalanceDetail.check = function () {

    var selected = mTable.checkStatus(TMonthbalanceDetail.id);
    // var selected = $('#' + this.id).bootstrapTable('getSelections');
    if (selected.data.length == 0 ) {
        Feng.info("请先选中表格中的某一记录！");
        return false;
    } else {
        TMonthbalanceDetail.seItem = selected.data[0];
        return true;
    }
};

/**
 * 点击添加账单
 */
TMonthbalanceDetail.openAddTMonthbalanceDetail = function () {
    var selected = mTable.checkStatus(TMonthbalanceDetail.id);
    if (selected.data.length == 1 ){
        var index = layer.open({
            type: 2,
            title: '账单详细',
            area: ['800px', '420px'], //宽高
            fix: false, //不固定
            maxmin: true,
            content: Feng.ctxPath + '/tMonthbalanceDetail/tMonthbalanceDetailList?flag='+selected.data[0].income+"&id="+selected.data[0].id+"&bId="+selected.data[0].monthbalance_id
        });
        layer.full(index)
        this.layerIndex = index;
    }else{
        Feng.info("请先选中表格中的某一记录！");
    }

};

/**
 * 打开查看账单详情
 */
TMonthbalanceDetail.openTMonthbalanceDetailDetail = function () {


    var traner='';
    var balance_num='';
    var selected = mTable.checkStatus(TMonthbalanceDetail.id);
    var ids = new Array();

    if(selected.data.length<=0){
        Feng.info("请选择结算对象！");
        return;
    }
    for (var i = 0; i < selected.data.length; i++) {
        if(selected.data[i].status==1){
            Feng.info("不能选择已结算！");
            return;
        }
        if(selected.data[i].traner!=selected.data[0].traner){
            Feng.info("只能选择相同的网点或收货人进行结算");
            return;
        }
        traner=selected.data[i].traner;
        ids.push(selected.data[i].id);
    }
    var sUrl=Feng.ctxPath + '/tMonthbalanceDetail/tMonthbalanceDetail_update?ids=' + ids.join(",")+"&name="+traner;
        
    if (this.check()) {
        var index = layer.open({
            type: 2,
            title: '账单详情',
            area: ['800px', '420px'], //宽高
            fix: false, //不固定
            maxmin: true,
            content:sUrl
        });
        layer.full(index)
        this.layerIndex = index;
    }
};

/**
 * 删除账单
 */
TMonthbalanceDetail.delete = function () {
    if (this.check()) {
        var ajax = new $ax(Feng.ctxPath + "/tMonthbalanceDetail/delete", function (data) {
            Feng.success("删除成功!");
            TMonthbalanceDetail.table.refresh();
        }, function (data) {
            Feng.error("删除失败!" + data.responseJSON.message + "!");
        });
        ajax.set("tMonthbalanceDetailId", this.seItem.id);
        ajax.start();
    }
};

TMonthbalanceDetail.group_search=function () {
    var queryData = {};
    queryData['name'] = $("#condition").val();
    queryData['status'] = $("#status").val()+"";
    queryData['beginTime'] = $("#beginTime").val();
    queryData['endTime'] = $("#endTime").val();
    var ajax = new $ax(Feng.ctxPath + "/tMonthbalanceDetail/listGroup", function (data) {
        console.log(data)
        $('#zzhj').text(data.money);
    }, function (data) {

    });
    ajax.setData(queryData)
    ajax.start();
}

/**
 * 查询账单列表
 */
TMonthbalanceDetail.search = function () {
    var queryData = {};
    queryData['name'] = $("#condition").val();
    queryData['status'] = $("#status").val()+"";
    queryData['beginTime'] = $("#beginTime").val();
    queryData['endTime'] = $("#endTime").val();
    aTable.refresh(queryData);
    TMonthbalanceDetail.group_search();
};

var aTable = null
var mTable = null;
$(function () {
    var defaultColunms = TMonthbalanceDetail.initColumn();

    var sUrl = "/tMonthbalanceDetail/list"
    aTable = new LYTable(TMonthbalanceDetail.id, sUrl, defaultColunms);
    layui.use('table', function () {
        mTable = layui.table;
        TMonthbalanceDetail.table = aTable.init(mTable);
    })


    $('#status').selectpicker({
        style: 'btn btn-select',
        noneSelectedText: "账单状态"
    });
    TMonthbalanceDetail.group_search();

});

$(document).keyup(function(event) {
    if (event.keyCode == 13) {
        TMonthbalanceDetail.search();
    }
});


